<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;
use think\model\relation\BelongsTo;
use think\model\relation\HasMany;

/**
 * @mixin \think\Model
 */
class AdminLeaveForm extends Model
{

    const STATE_PROCESSING = "processing"; // 正在审批
    const STATE_APPROVED = "approved"; // 审批通过
    const STATE_REFUSED = "refused"; // 审批驳回

    const STATE_ARR = ['processing'=>'正在审批','approved'=>'审批通过','refused'=>'审批驳回'];
    const FORM_TYPE_ARR = [0=>'事假',1=>'病假'];

    //
    protected $autoWriteTimestamp = true;

    public function school() {
        return $this->belongsTo(AdminSchool::class, 'school_id')
            ->where('is_delete', 0)->bind([
                'school_name' => 'name'
            ]);
    }

    public function staff(): BelongsTo {
        return $this->belongsTo(AdminStaff::class, 'staff_id')->bind([
            'staff_name' => 'name',
            'staff_type' => 'type',
            'staff_type_text' => 'type_text',
            'student_no',
            'sex',
            'phone',
        ]);
    }

    public function leaveProcessFlow(): hasMany {
        return $this->hasMany('AdminLeaveProcessFlow', 'leave_form_id', 'id')
            ->hidden(['is_delete','update_time'])->append(['state_text','action_text','result_text']);
    }

    public function getStateTextAttr($value,$data)
    {
        $type = self::STATE_ARR;
        return $data['state'] ? $type[$data['state']] : '';
    }

    public function getFormTypeTextAttr($value,$data)
    {
        $type = self::FORM_TYPE_ARR;
        return $type[$data['form_type']];
    }

    public function getStartTimeAttr($value) {
        return date('Y-m-d H:i:s', $value);
    }
    public function getEndTimeAttr($value) {
        return date('Y-m-d H:i:s', $value);
    }
}
